package cn.edu.lingnan.tourism.utils;
import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author liyinglai
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    //返回信息码
    private String code;
    //返回信息
    private String msg;
    private PageInfo pageInfo;
    //返回数据
    T data;


    //成功，只返回成功码和信息
    public String success(String msg){
        Result<T> result=new Result<>();
        result.setCode(ResultCode.SUCCESS.code);
        result.setMsg(msg);
        return JSON.toJSONString(result);
    }

    //成功，返回成功码、信息和数据
    public String success(T data){
        Result<T> result=new Result();
        result.setCode(ResultCode.SUCCESS.code);
        result.setMsg(ResultCode.SUCCESS.msg);
        result.setData(data);
        return JSON.toJSONString(result);
    }
    public String success(PageInfo pageInfo){
        Result<T> result=new Result();
        result.setCode(ResultCode.SUCCESS.code);
        result.setMsg(ResultCode.SUCCESS.msg);
      result.setPageInfo(pageInfo);
        return JSON.toJSONString(result);
    }
    public String success(String msg,T data){
        Result<T> result=new Result();
        result.setCode(ResultCode.SUCCESS.code);
        result.setMsg(msg);
        result.setData(data);
        return JSON.toJSONString(result);
    }
    //失败，返回controller层传过来信息码和信息
    public String error(String msg){
        Result<T> result=new Result<>();
        result.setCode(ResultCode.ERROR.code);
        result.setMsg(msg);
        return JSON.toJSONString(result);
    }

}

